'use strict';
var _l = require('lodash');
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/booths/kh/getBoothsInfo 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------

		let booth = await vk.baseDao.selects({
			dbName: "bidding-booths", // 主表名
			getOne: true, // 只返回第一条数据
			getMain: true,
			whereJson: {
				_id: data._id,
			},
			// 副表列表
			foreignDB: [{
				dbName: "bidding-biddingprices", // 副表名
				localKey: "_id", // 主表外键字段名
				foreignKey: "booth_id", // 副表外键字段名
				as: "biddingList",
				limit: 200, // 当limit = 1时，以对象形式返回，否则以数组形式返回
				whereJson: { // 条件
					status: _.neq(-1)
				},
				// 副表字段显示规则
				fieldJson: {},
				// 副表排序规则
				sortArr: [{
					"name": "price",
					"type": "desc"
				}],

			},
			{
				dbName: "bidding-session", // 副表名
				localKey: "location", // 主表外键字段名
				foreignKey: "place_id", // 副表外键字段名
				as: "sessions",
				limit: 1000, // 当limit = 1时，以对象形式返回，否则以数组形式返回
			}
			]
		});
		if (vk.pubfn.isNull(booth.nowPrice)) {
			booth.nowPrice = booth.basePrice;
		}
		if (vk.pubfn.isNull(booth.status)) {
			booth.status = 0;
		}
		let status3 = _l.filter(booth.sessions, x => x.status == 3), maxsession;
		if (vk.pubfn.isNotNull(status3)) {
			maxsession = _l.maxBy(status3, y => y.end);
		}


		if (vk.pubfn.isNull(booth.status) || booth.status < 3) {
			if (vk.pubfn.isNotNull(booth.sessions.find(x => x.status == 2))) {
				booth.session = booth.sessions.find(x => x.status == 2)
			} else if (vk.pubfn.isNotNull(booth.sessions.find(x => x.status == 1))) {
				booth.session = booth.sessions.find(x => x.status == 1)
			} else {
				console.log('??')
				booth.session = maxsession;
			}
			if (vk.pubfn.isNull(booth.endtime) || (vk.pubfn.isNull(booth.inLast) || !booth.inLast) && vk.pubfn.isNotNull(booth.session)) {
				booth.endtime = booth.session.end;
			} else {
				booth.endtime = booth.endtime
			}
		} else {

			booth.session = maxsession;
		}


		res.data = booth;
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}